package com.example.duanxin;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import cn.smssdk.EventHandler;
import cn.smssdk.OnSendMessageHandler;
import cn.smssdk.SMSSDK;

public class MainActivity<handler> extends AppCompatActivity {
    private Button send;
    private EditText phone;
    private EditText yanzhengma;
    private EditText password;
    private EditText password_yanzheng;
    private String phone_input;
    private Button zhuce;
    private String yanzheng_number;
    private CountDownTimerUtils countDownTimer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhuce);
        send  = findViewById(R.id.send);//发送验证码按钮
        phone = findViewById(R.id.phone);//手机号编辑框
        yanzhengma = findViewById(R.id.yanzhengma);//验证码编辑框
        password = findViewById(R.id.password);//密码编辑框
        password_yanzheng = findViewById(R.id.password_yanzheng);//再次输入密码编辑框
        zhuce = findViewById(R.id.zhuche);//注册按钮


        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 发送成功进入倒计时
                countDownTimer = new CountDownTimerUtils(send, 60000, 1000);
                countDownTimer.start();
                phone_input = phone.getText().toString();
                //获取验证码
                if (TextUtils.isEmpty(phone_input))
                    Toast.makeText(MainActivity.this,"号码不能为空",Toast.LENGTH_SHORT).show();
                Log.i("1234",phone_input.toString());
                SMSSDK.getVerificationCode("86",phone_input, (OnSendMessageHandler) null);
            }
        });

        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //提交验证码验证
                phone_input = phone.getText().toString();
                if (TextUtils.isEmpty(phone_input))
                    Toast.makeText(MainActivity.this,"号码不能为空",Toast.LENGTH_SHORT).show();
                yanzheng_number = yanzhengma.getText().toString();
                if (TextUtils.isEmpty(yanzheng_number))
                    Toast.makeText(MainActivity.this,"号码不能为空",Toast.LENGTH_SHORT).show();
                SMSSDK.submitVerificationCode("86",phone_input,yanzheng_number);
            }
        });


        EventHandler eh=new EventHandler(){

            @Override
            public void afterEvent(int event, int result, Object data) {

                if (result == SMSSDK.RESULT_COMPLETE) {

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,"验证码已发送",Toast.LENGTH_SHORT).show();
                        }
                    });
                    //回调完成

                    // 处理成功得到验证码的结果
                    // 请注意，此时只是完成了发送验证码的请求，验证码短信还需要几秒钟之后才送达
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
                        //提交验证码成功
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this,"验证成功",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,"验证码错误",Toast.LENGTH_SHORT).show();
                        }
                    });
                    ((Throwable)data).printStackTrace();
                }
            }
        };
        SMSSDK.registerEventHandler(eh); //注册短信回调
    }

}
